﻿using AvalonDock.Layout;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
using SqLiteEditor.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace SqLiteEditor.Templates
{

    

    //SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows 
    //the user to rename a table or to add a new column to an existing table. 
    //It is not possible to rename a column, remove a column, or add or remove constraints from a table.
    //You can:

    //create new table as the one you are trying to change,
    //copy all data,
    //drop old table,
    //rename the new one.



    /// <summary>
    /// Interaction logic for TStructureWindow.xaml
    /// </summary>
    public partial class TStructureWindow : UserControl
    {

        public TStructureWindow()
        {
            InitializeComponent();
            InitializeSyntaxHighlighting();
            SqliteStructureDataGrid.SelectedCellsChanged += SqliteStructureDataGrid_SelectedCellsChanged;
            SqliteStructureDataGrid.SelectionChanged += SqliteStructureDataGrid_SelectionChanged;
            SqliteStructureDataGrid.CurrentCellChanged += SqliteStructureDataGrid_CurrentCellChanged;
            SqliteStructureDataGrid.RowEditEnding += SqliteStructureDataGrid_RowEditEnding;
            LayoutRoot.DataContext = this;
        }

        void SqliteStructureDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {

  //          List<DataGridCellInfo> deletedrows = e.RemovedCells; 

            if (e.RemovedCells.Count > 0)
            {
                
            }
        }

        void SqliteStructureDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        void SqliteStructureDataGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        { 
        }

        void SqliteStructureDataGrid_CurrentCellChanged(object sender, EventArgs e)
        {
           
        }

        private ObservableCollection<TTabelStructure> mTableStructureDataSource  = new ObservableCollection<TTabelStructure>();
        public ObservableCollection<TTabelStructure> TableStructureDataSource
        {
            get { return mTableStructureDataSource; }
            set { mTableStructureDataSource  = value; }
        }

        private void InitializeSyntaxHighlighting()
        {
            using (Stream s = TStreamHelper.OpenStream("Sql.xshd"))
            {
                using (XmlTextReader reader = new XmlTextReader(s))
                {
                    QueryEdittor.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
                }
            }
        }
           

        public void GetDesignTable(string tabelname)
        {
            TableStructureDataSource.Clear();
            List<TTabelStructure> Fields = SqliteHelper.GetColumnFieldType(tabelname);

                        
            //foreach (TTabelStructure field in Fields)
            //{
                TableStructureDataSource = new ObservableCollection<TTabelStructure>(Fields.ToList());
            //}

            SqliteStructureDataGrid.ItemsSource = TableStructureDataSource;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SqliteHelper.RenameColumn("Foods", "Food", "Foodaa");
        }

    }
}
